package com.aphrodite.io.sip.header;

import java.util.Vector;

import com.aphrodite.util.StringParser;

/** Generic SIP Header containing a list of tokens (Strings). */
public abstract class ListHeader extends Header {
	public ListHeader(Header hd) {
		super(hd);
	}

	public ListHeader(String hname, String hvalue) {
		super(hname, hvalue);
	}

	/** Adds a new token to the elements list. */
	public void addElement(String elem) {
		if (value == null || value.length() == 0)
			value = elem;
		else
			value += ", " + elem;
	}

	/** Gets list of tokens (as Vector of Strings). */
	public Vector getElements() {
		Vector elements = new Vector();
		StringParser par = new StringParser(value);
		char[] delim = { ',' };
		while (par.hasMore()) {
			String elem = par.getWord(delim).trim();
			if (elem != null && elem.length() > 0)
				elements.addElement(elem);
			par.skipChar();
		}
		return elements;
	}

	/** Sets the list of tokens. */
	public void setElements(Vector elements) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < elements.size(); i++) {
			if (i > 0)
				sb.append(", ");
			sb.append((String) elements.elementAt(i));
		}
		value = sb.toString();
	}
}
